<?php
function_exists('UnixPath')  || die('OK');
	//$DB->_tab = "_super_left";

	$ACT = array('list', 'add', 'aok', 'edit', 'eok', 'del');
	in_array($_GET['act'], $ACT) || $_GET['act'] = 'list';
	$html['tpl_right_act'] = $_GET['act'];

	$_GET['id']	= (int) $_GET['id'];
	$_GET['pid']	= (int) $_GET['pid'];

	$P = E::$DB->one('_super_left', 'id=' . $_GET['pid']);
	$P['id'] || $P = array('id'=>0, 'name'=>'根节点', 'deep'=>'0');

	$body['navlist'] = array(
		'list'	=>	'左侧栏列表',
		'add'	=>	'添加左侧栏',
	);

	$body['_url']	= '&pid='.$P['id'];
	$body['url']	=  $html['url'] . $body['_url'];

/**********php cache set*******/
	$order		= 'ORDER BY deep ASC, pid ASC, orderId DESC, id ASC';
	$cache_file	= 'left';
/****************/

	$body['node_tree'] = node_tree($P); 



if( $_GET['act'] == 'add' ){

}

if( $_GET['act'] == 'aok' ){
	if( $P['deep']=='3' && $_POST['isParent'] ){
		A::msg('不能添加父节点了!',  '', 1, 1);
	}
	$_POST['deep'] = $P['deep'] + 1;
	$_POST['pid'] = $P['id'];

	E::$DB->insert('_super_left', 'deep, isParent, isHide, pid, orderId, name, mod, fun, act , tail, detail');
	C::tree('_super_left', $order, $cache_file);
	C::leftTitle();
	A::msg('插入节点完成!', $_POST['jurl'], 3, 1 );
}

if( $_GET['act'] == 'edit' ){
	//$html['tpl_right_act'] = 'add';
	$L = E::$DB->one('_super_left', 'id=' . $_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1 );
	$L['isCanChangeParent'] = count( E::$DB->all('_super_left', 'pid=' . $L['id'] ) );
}


if( $_GET['act'] == 'eok' ){
	$L = E::$DB->one('_super_left', 'id=' . $_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1 );

	$str = 'isHide, orderId, name, mod, fun, act , tail, detail ';
	if( !count(E::$DB->all('_super_left', 'pid=' . $L['id'] )) ){
		$str .= ',isParent';
	}
	E::$DB->update('_super_left', $str, 'id=' . $L['id']);
	$order =' ORDER BY orderId DESC , id ASC';
	C::tree('_super_left', $order, $cache_file);
	C::leftTitle();
	A::msg('修改节点 ' .$_POST['name'] . '  完成!', $_POST['jurl'], 3, 1 );
}


if( $_GET['act'] == 'del' ){
	$L = E::$DB->one('_super_left', 'id=' . $_GET['id']);
	$L['id'] || A::msg('无效的节点', $_POST['jurl'], 1, 1);

	if(count(E::$DB->all('_super_left', 'pid=' . $L['id'] ))){
		A::msg('您必须先删除该节点的子节点!',  '',1,1);
	}
	E::$DB->query('DELETE FROM  _super_left WHERE id=' . $L[id]);
	C::tree('_super_left', $order, $cache_file);
	C::leftTitle();
	A::msg('删除节点 ' . $L['name']  . '完成!', $_POST['jurl'], 3, 1 );
}


if( $_GET['act']=="list" ){
	$p = E::$DB->page('_super_left', 'pid=' . $P['id'] );
	$L = E::$DB->all('_super_left', 'pid=' . $P['id'] .' ORDER BY isHide ASC, orderId DESC ,id ASC '. $p['limit']);
}





function node_tree($p){
	global $html;
	if( !$p['id'] ) {
		return '<a href="'.$html['url'].'&pid=0&act=list">根节点</a>';
	}
	$s = '<a href="' . $html['url'] . '&pid='.$p['id'].'&act=list">' . $p['name'] . '</a>';
	if( $p['pid'] ){
		$s = node_tree(E::$DB->one('_super_left', 'id=' . $p['pid'])) .' <small>&#187;</small> '. $s;
	}else{
		$s = '<a href="'.$html['url'].'&pid=0&act=list">根节点</a> <small>&#187;</small> '. $s;
	}
	return $s;
}

